package fr.lox.fall.common;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;

public class Player implements InputProcessor{
	
	private Vector2 _position = new Vector2();
	private Sprite _spr;
	private float _life;
	private float _distance, degrees = 0;
	private boolean _isRun = false;
	private Texture _tex;
	private TextureRegion _currentFrame;
	private float _speed = 8f, ratioX = Gdx.graphics.getWidth() * 0.5f, ratioY = Gdx.graphics.getHeight() * 0.5f;
	//private Bonus _bonus;
	
	//  <   > 
	
	public Player(float life)
	{
		this._tex = new Texture(Gdx.files.internal("images/charset/player.png"));
		_spr = new Sprite();
		TextureRegion[][] spl = TextureRegion.split(this._tex, 16, 32);
		this._currentFrame = spl[0][0];
		this._life = life;
		this._distance = 0f;
		this._position.x = (ratioX * 0.28f) * 1f;
		this._position.y = (ratioY * 0.8f) * 2f;
		_spr.setSize(16, 32);
		_spr.setTexture(_tex);
		_spr.setRegion(_currentFrame);
		_spr.setColor(0, 0, 1, 1);
	}
	
	public float getX()
	{
		return (this._position.x);
	}
	
	public float getY()
	{
		return (this._position.y);
	}
	
	public void dispose()
	{
		_tex.dispose();
	}
	
	public void resize(int width, int height)
	{
		this._spr.setSize(width * 0.04f, height * 0.05f);
		ratioX = width;
		ratioY = height;
	}
	
	public void run()
	{
		if (degrees > -10.7)
		{
		 this.degrees -= 0.7;
		 this._spr.rotate(degrees);
		}
		
		if (this._position.x < ratioX * 0.3)
			this._position.x += 5f;
		if (this._position.y > Gdx.graphics.getHeight() / 2)
			this._position.y -= _speed;
	}
	
	public void move()
	{
		if (Gdx.input.isTouched())
		{
			if (Gdx.input.getX() < (Gdx.graphics.getWidth() / 2))
			{
				this._position.x -= (0.01 * ((Gdx.graphics.getWidth() / 2) - Gdx.input.getX()));
			}
			if (Gdx.input.getX() > (Gdx.graphics.getWidth() / 2))
			{
				this._position.x += (0.01 * (Gdx.input.getX() - (Gdx.graphics.getWidth() / 2)));
			}
		}
	}
	
	public void update()
	{
		if (Gdx.input.isTouched())
			this._isRun = true;
		if (this._isRun)
		{
			run();
			this.move();
		}
		this._spr.setPosition(this._position.x, this._position.y);
		
	}
	
	public void draw(SpriteBatch batch)
	{
		//batch.draw(this._currentFrame, this._position.x, this._position.y);
		this._spr.draw(batch);
	}

	@Override
	public boolean keyDown(int keycode) {
		// TODO Auto-generated method stub
		if (keycode == Keys.E)
		{
			this._isRun = true;
			System.out.println("fuck");
		}
		return false;
	}

	@Override
	public boolean keyUp(int keycode) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean keyTyped(char character) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchDown(int screenX, int screenY, int pointer, int button) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchUp(int screenX, int screenY, int pointer, int button) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchDragged(int screenX, int screenY, int pointer) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean mouseMoved(int screenX, int screenY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean scrolled(int amount) {
		// TODO Auto-generated method stub
		return false;
	}

}
